<?php
/**
 *  后台关于我们的管理
 * Author: lyun
 * Date: 2018/1/27
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin;

use App\Http\Models\Admin\AboutUs;
use App\Http\Requests\Admin\AboutUsStoreRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use App\Services\Utils\DeleteRichTextBosKey;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AboutUsController extends Controller
{
    use ApiChecker,ApiResponser;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $about_us = AboutUs::all();

        return $this->response(compact('about_us'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  AboutUsStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AboutUsStoreRequest $request)
    {
        $about_us_data = $this->transformAboutUsData($request);

        $stored = AboutUs::create($about_us_data);

        return $this->response(compact('stored'));
    }

    /**
     * @param AboutUsStoreRequest $request
     * @return array
     */
    private function transformAboutUsData(AboutUsStoreRequest $request)
    {
        return [
            'telephone'         => $request->telephone,
            'email'             => $request->email,
            'qq'                => $request->qq,
            'address'           => $request->address,
            'time'              => $request->time,
            'company'           => $request->company,
            'fax'               => $request->fax,
            'website'           => $request->website,
            'business_concepts' => $request->business_concepts,
            'business_miss'     => $request->business_miss,
            'business_spirit'   => $request->business_spirit,
            'about_us'          => $request->about_us,
            'contact_us'        => $request->contact_us,
        ];
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  AboutUsStoreRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \App\Exceptions\AdminManageException
     */
    public function update(AboutUsStoreRequest $request, $id)
    {
        /** @var AboutUs $about_us */
        $about_us = (new AboutUs)->find($id);
            $this->checkItemExist($about_us, '关于我们的信息');

        $about_us_data = $this->transformAboutUsData($request);

        $this->updateProductionBosKey($about_us_data, $about_us); // 更新富文本中的boskey信息

        $updated = $about_us->update($about_us_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \App\Exceptions\AdminManageException
     * @throws \Exception
     */
    public function destroy($id)
    {
        /** @var AboutUs $about_us */
        $about_us = (new AboutUs)->find($id);
            $this->checkItemExist($about_us, '关于我们的信息');

        // 删除富文本的boskey的值
        (new DeleteRichTextBosKey)->deleteRichTextBosKey($about_us->contact_us);
        (new DeleteRichTextBosKey)->deleteRichTextBosKey($about_us->about_us);

        $deleted = $about_us->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param array $about_us_data
     * @param AboutUs $about_us
     */
    private function updateProductionBosKey(array $about_us_data, AboutUs $about_us)
    {
        // 更新富文本中的图片，删除原来的boskey
        $new_about_us = $about_us_data['about_us'];
        $old_about_us = $about_us->about_us;
        (new DeleteRichTextBosKey)->updateRichTextBosKey($new_about_us, $old_about_us);

        $new_contact_us = $about_us_data['contact_us'];
        $old_contact_us = $about_us->contact_us;
        (new DeleteRichTextBosKey)->updateRichTextBosKey($new_contact_us, $old_contact_us);
    }
}
